<?php
namespace Components\Account\User\Info;

use Components\Account\User\Adapter;

class Validation extends \Phalcon\Validation {
  public function execute($data) {
    $validationMessages = $this -> validate($data);
    if($validationMessages -> valid()) {
      return ['code'=>-1,'message'=>$validationMessages[0]->getMessage()];
    }

    $resData = [];
    foreach($this -> getValidators() as $v) {

      if(isset($data[$v[0]]) && (!empty($data[$v[0]]) || $data[$v[0]] == '0')) {
        $filterFunName = 'check' . ucfirst($v[0]);

        if(strstr($filterFunName, '_code')) {
          if(isset($data['province_code']) && $data['province_code'] == 0) {
            $resData['address_code'] = 0;
          } else if(isset($data['city_code']) && $data['city_code'] == 0) {
            $resData['address_code'] = $data['province_code'];
          } else if(isset($data['district_code']) && $data['district_code'] == 0) {
            $resData['address_code'] = $data['city_code'];
          } else {
            $resData['address_code'] = $data['district_code'];
          }
        } else {
          if(!method_exists(Adapter::getInfoFilterInstance(), $filterFunName)) { return ['code'=>-1,'message'=>'请联系开发人员:'.get_class(Adapter::getInfoFilterInstance()) . '->' . $filterFunName . '不存在!']; }
          $resData[$v[0]] = Adapter::getInfoFilterInstance() -> $filterFunName($data[$v[0]]);
        }
      }
    }

    return ['code'=>0,'data'=>$resData];
  }

  public function addSex() {
    return $this -> rules('sex', [
      new \Phalcon\Validation\Validator\InclusionIn([
        'domain' => [0, 1, 2]
      ])
    ]);
  }

  public function addAddressCodeByDistpicker() {
    $this -> rules('province_code', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误province_code',
        'callback' => function($data) {
          return true;
        }
      ])
    ]);
    $this -> rules('city_code', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误city_code',
        'callback' => function($data) {
          return true;
        }
      ])
    ]);
    $this -> rules('district_code', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误district_code',
        'callback' => function($data) {
          return true;
        }
      ])
    ]);
    return $this;
  }

  public function addAddressCode() {
    return $this -> rules('address_code', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误address_code',
        'callback' => function($data) {
          if(!isset($data['address_code']) || empty($data['address_code'])) {
            return true;
          }

          return true;
        }
      ])
    ]);
  }

  public function addAddressDetail() {
    return $this -> rules('address_detail', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误address_detail',
        'callback' => function($data) {
          if(!isset($data['address_detail']) || empty($data['address_detail'])) {
            return true;
          }

          return true;
        }
      ])
    ]);
  }

  public function addIdentityCard() {
    return $this -> rules('identity_card', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误identity_card',
        'callback' => function($data) {
          if(!isset($data['identity_card']) || empty($data['identity_card'])) {
            return true;
          }

          return true;
        }
      ])
    ]);
  }

  public function addBankName() {
    return $this -> rules('bank_name', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误bank_name',
        'callback' => function($data) {
          if(!isset($data['bank_name']) || empty($data['bank_name'])) {
            return true;
          }

          return true;
        }
      ])
    ]);
  }

  public function addBankAccount() {
    return $this -> rules('bank_account', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误bank_account',
        'callback' => function($data) {
          if(!isset($data['bank_account']) || empty($data['bank_account'])) {
            return true;
          }

          return true;
        }
      ])
    ]);
  }

  public function addBankAccountName() {
    return $this -> rules('bank_account_name', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误bank_account_name',
        'callback' => function($data) {
          if(!isset($data['bank_account_name']) || empty($data['bank_account_name'])) {
            return true;
          }

          return true;
        }
      ])
    ]);
  }

  public function addBankDeposit() {
    return $this -> rules('bank_deposit', [
      new \Phalcon\Validation\Validator\Callback([
        'message' => '未定义错误bank_deposit',
        'callback' => function($data) {
          if(!isset($data['bank_deposit']) || empty($data['bank_deposit'])) {
            return true;
          }

          return true;
        }
      ])
    ]);
  }
}
